package com.baihan.angus.app.game.busshow.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.PicBean;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.baihan.angus.app.game.common.util.media.DefMediaInfo;
import com.baihan.angus.app.game.common.util.media.IMediaInfo;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.ArrayList;
import java.util.List;

public class PhotoListAdp extends BaseAdp<PicBean, PhotoListAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    private List<PicBean> picList = new ArrayList<>();

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        PicBean data = getItem(position);

        ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                vh.ivPhoto, data.getUrl(), R.mipmap.ic_launcher, 0);

        List<IMediaInfo> list = new ArrayList<>();
        for (int k = 0; k < picList.size(); k++) {
            list.add(new DefMediaInfo(picList.get(k).getUrl()));
        }

        vh.ivPhoto.setOnClickListener(view -> {
            mOnItemClickListener.preview(position, list);
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public void setPicList(List<PicBean> picList) {
        this.picList = picList;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        QMUIRadiusImageView ivPhoto;
        LinearLayout llItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.ivPhoto);
            llItem = itemView.findViewById(R.id.llItem);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void preview(int index, List<IMediaInfo> list);
    }
}
